package com.jiandp.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.jiandp.dto.Result;
import com.jiandp.entity.ShopType;
import com.jiandp.mapper.ShopTypeMapper;
import com.jiandp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.rmi.CORBA.Util;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<ShopType> getList() {
        String key="login:type";
        //查询redis中是否存在
        String shopTypeJson = stringRedisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(shopTypeJson)){
            //存在直接返回
            return JSONUtil.toList(shopTypeJson,ShopType.class);
        }
        //不存在查询数据库
        List<ShopType> typeList = this.query().orderByAsc("sort").list();
        //数据库不存在，返回错误
        if (typeList.isEmpty()){
            return new ArrayList<>();

        }
        //数据库中存在，保存到redis中，并返回
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(typeList));
        // 7. 返回
        return typeList;
    }
}
